package com.itlaoqi.thread;

import java.util.Random;

public class SyncSample {
    public static void main(String[] args) {
        Couple c = new Couple();
        for (int i = 0;i<10000;i++){
            new Thread(){
                @Override
                public void run() {
                    int r = new Random().nextInt(2);
                    if(r % 2 == 0){
                        c.first();
                    }else {
                        c.second();
                    }
                }
            }.start();
        }
    }

}
class Couple{

    Object lock = new Object();

    public void first(){
        synchronized (lock) {
            System.out.printf("琴");
            System.out.printf("瑟");
            System.out.printf("琵");
            System.out.printf("琶");
            System.out.println();
        }
    }

    public void second(){
        synchronized (lock) {
            System.out.printf("魑");
            System.out.printf("魅");
            System.out.printf("魍");
            System.out.printf("魉");
            System.out.println();
        }
    }
}
